package com.bangdream.michelia.view.fragment.currency;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.bangdream.michelia.R;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.MyLog;

/**
 * Created by Administrator on 2016/8/22.
 * 首页选项卡的所有webView页
 */
public class WebViewFragment extends BackHandledFragment implements View.OnClickListener{
	private static final String TAG = "WebViewFragment";
    private View v;
	private WebView webview;
	private String url;
	private ProgressBar pb;										//webView加载进度条
	@SuppressLint("SetJavaScriptEnabled")
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.webview, container, false);
        Bundle b = getArguments();
        if(b!=null) {
			url = b.getString("url");
		}
		iniView();
		iniTextDialog();
        return v;
    }


	/**
	 * 初始化弹窗
	 */
	private void iniTextDialog()
	{

	}
	@JavascriptInterface
	private void iniView()
    {
		pb =  v.findViewById(R.id.pb);
		webview = v.findViewById(R.id.webview);
		webview.loadUrl(url);
		pb.setMax(100);
		// 设置可以访问文件
		//webView.getSettings().setAllowFileAccess(true);
		//取消滚动条
		//webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true); 			// 启用支持javascript
		webSettings.setLoadWithOverviewMode(true);			//这方法可以让你的页面适应手机屏幕的分辨率，完整的显示在屏幕上，可以放大缩小。
		webSettings.setUseWideViewPort(true);				//关键点
		webSettings.setAllowFileAccess(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		webSettings.setDisplayZoomControls(false);
		webSettings.setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
		webSettings.setSupportZoom(false);		//页面支持缩放
		//webSettings.setSupportZoom(true);
		//webSettings.setBuiltInZoomControls(true);
		webSettings.supportMultipleWindows();  //多窗口

		webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
				//判断是否为跳转到了打开附件的路径 是的话则调用下载的方法
				if(url.contains("PowerPlat/FormXML/FileViewer.aspx?pdfurl="))
				{
					url += "&online=1";
					MyLog.e(TAG,"URL====="+url);
				}
				else {

				}
				//  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
				view.loadUrl(url);
                return true;
            }
            public void onPageFinished(WebView view, String url) {
                CookieManager cookieManager = CookieManager.getInstance();
                String CookieStr = cookieManager.getCookie(url);
                super.onPageFinished(view, url);
            }
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				MyLog.d(TAG,"开始加载");
				super.onPageStarted(view, url, favicon);
			}
			@Override
			public void onScaleChanged(WebView view, float oldScale, float newScale) {
				super.onScaleChanged(view, oldScale, newScale);
			}
		});
		//设置进度条
		webview.setWebChromeClient(new WebChromeClient(){
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				pb.setProgress(newProgress);
				if(newProgress==100){
					pb.setVisibility(View.GONE);
				}
				super.onProgressChanged(view, newProgress);
			}
		});
    }

	@Override
	public void onClick(View view) {
		switch (view.getId())
		{
			default:
				break;
		}
	}


	//使用Webview的时候，返回键没有重写的时候会直接关闭程序，这时候其实我们要其执行的知识回退到上一步的操作
	@Override
	public boolean onBackPressed() {
		//这是一个监听用的按键的方法，keyCode 监听用户的动作，如果是按了返回键，同时Webview要返回的话，WebView执行回退操作，因为mWebView.canGoBack()返回的是一个Boolean类型，所以我们把它返回为true
		if(webview.canGoBack()) {
			webview.goBack();
			return true;
		}
		return false;
	}

	@Override
	public BasePresenter createPresenter() {
		return null;
	}
}
